package com.android.meige.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.meige.R;
import com.android.meige.app.BaseActivity;
import com.android.meige.fragment.detail.Fragment_BrandGoodsList;
import com.android.meige.fragment.detail.Fragment_Detail_GoodsDetail_Comment;
import com.android.meige.fragment.detail.Fragment_Store_Info;
import com.android.meige.fragment.detail.Fragment_TabBrandGoodsList;
import com.android.meige.fragment.group.Fragment_BrandClearance;
import com.android.meige.fragment.group.Fragment_BrandDiscountMain;
import com.android.meige.fragment.group.Fragment_CFundingTaste;
import com.android.meige.fragment.group.Fragment_ChoiceTopic;
import com.android.meige.fragment.group.Fragment_ColonelFree;
import com.android.meige.fragment.group.Fragment_EveryBodyBuy;
import com.android.meige.fragment.group.Fragment_FreePostage;
import com.android.meige.fragment.group.Fragment_Main_Type_Result;
import com.android.meige.fragment.group.Fragment_Main_WebView;
import com.android.meige.fragment.group.Fragment_SACountry;
import com.android.meige.fragment.group.Fragment_SeaAmoyLClIST;
import com.android.meige.fragment.group.Fragment_StoreStreet;
import com.android.meige.fragment.group.Fragment_ThematicAList;
import com.android.meige.fragment.group.Fragment_group_BrandNation_Sort;
import com.android.meige.fragment.member.Fragment_Mine_AboutUs;
import com.android.meige.fragment.member.Fragment_Mine_Address;
import com.android.meige.fragment.member.Fragment_Mine_Detail;
import com.android.meige.fragment.member.Fragment_Mine_Express;
import com.android.meige.fragment.member.Fragment_Mine_Fans;
import com.android.meige.fragment.member.Fragment_Mine_Lottery;
import com.android.meige.fragment.member.Fragment_Mine_Message;
import com.android.meige.fragment.member.Fragment_Mine_Money;
import com.android.meige.fragment.member.Fragment_Mine_Money_Cash;
import com.android.meige.fragment.member.Fragment_Mine_Rank;
import com.android.meige.fragment.member.Fragment_Mine_Rate;
import com.android.meige.fragment.member.Fragment_Mine_Score_Detail;
import com.android.meige.fragment.member.Fragment_Mine_TeamDetail;
import com.android.meige.fragment.member.Fragment_SameCity_Service;
import com.android.meige.pojo.BannerUintPojo;
import com.android.meige.utils.ConstantsUrl;
import com.android.meige.utils.JumpUtils;
import com.android.meige.utils.LoginState;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BaseObject extends BaseActivity implements Fragment_Main_WebView.CallBackValue {


    @BindView(R.id.main_top_bg)
    Toolbar mainTopBg;
    @BindView(R.id.main_top_share)
    ImageView main_top_share;
    @BindView(R.id.main_top_citychoose)
    public TextView main_top_citychoose;
    @BindView(R.id.main_top_title)
    public TextView main_top_title;
    private String title;

    private BaseObject mcontext;
    private String returncity="";
    private String return_type;
    private String web_url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_object);
        ButterKnife.bind(this);
        mcontext=BaseObject.this;
//        main_top_share.setVisibility(View.VISIBLE);
        title = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE);
        return_type = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.RETURN_TYPE);
        initTitle(0, title);
        initUI();
    }
    @Override
    public void onResume()
    {
        super.onResume();
     //   initUI();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 3){
            returncity = LoginState.getInstance().getUserInfo(mcontext, 3);
            main_top_citychoose.setText(returncity);
            if(!TextUtils.isEmpty(returncity)){
                main_top_title.setVisibility(View.GONE);
            }
            main_top_citychoose.setTextColor(Color.parseColor("#000000"));
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @OnClick({R.id.main_top_citychoose})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_top_citychoose:
                int requestCode =3;
                startActivityForResult(new Intent(mcontext, CityChoose.class),requestCode);
                break;
        }
    }
    private void initUI() {
        String need_id = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_NEED_ID);
        if(return_type !=null) {
            BannerUintPojo Apojo = new BannerUintPojo();
            Apojo.return_type = return_type;
            Apojo.title = title;
            Apojo.id = need_id;
            if(JumpUtils.jumpToEveryWhere(this, Apojo, null))return;
        }

        FragmentManager fm =  getSupportFragmentManager();
        switch (title) {
            case "客服":
            case "":
                mainTopBg.setVisibility(View.GONE);
                web_url = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_WEB_URL);
                String web_content = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_WEB_CONTENT);
                fm.beginTransaction().replace(R.id.frament, Fragment_Main_WebView.newInstance(web_url, web_content)).commit();
                break;
            case "品牌团":
                fm.beginTransaction().replace(R.id.frament, Fragment_group_BrandNation_Sort.newInstance(0)).commit();
                break;
            case "海淘团":
                fm.beginTransaction().replace(R.id.frament, Fragment_group_BrandNation_Sort.newInstance(1)).commit();
                break;
            case "店铺详情":
                fm.beginTransaction().replace(R.id.frament, Fragment_Store_Info.newInstance(need_id)).commit();
                break;
            case "试用结果":
            case "中奖名单":
                fm.beginTransaction().replace(R.id.frament, Fragment_Main_Type_Result.newInstance(need_id)).commit();
                break;
            case "用户评价":
                fm.beginTransaction().replace(R.id.frament, Fragment_Detail_GoodsDetail_Comment.newInstance(need_id)).commit();
                break;

            //个人中心
            case "收货地址":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Address.newInstance(need_id)).commit();
                break;
            case "我的佣金":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Money.newInstance(0)).commit();
                break;
            case "提现记录":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Money.newInstance(1)).commit();
                break;
            case "提现":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Money_Cash.newInstance(need_id)).commit();
                break;
            case "我的抽奖":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Lottery.newInstance()).commit();
                break;
            case "我的消息":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Message.newInstance()).commit();
                break;
            case "评价":
                String good_id = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_RATE_GOOD_ID);
                String id = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_RATE_ID);
                String buy_num = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_RATE_BUY_NUM);
                String goods_spec = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_RATE_GOODS_SPEC);
                String thumb = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_RATE_THUMB);
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Rate.newInstance(thumb, buy_num, good_id, id, goods_spec)).commit();
                break;
            case "物流详情":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Express.newInstance(need_id)).commit();
                break;
            case "订单详情":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Detail.newInstance(need_id)).commit();
                break;
            case "团详情":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_TeamDetail.newInstance(need_id)).commit();
                mainTopBg.setVisibility(View.GONE);
                break;
            case "拼团王":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Rank.newInstance()).commit();
                break;
            case "专题活动":
                String topicname = getIntent().getStringExtra("topicname");
                initTitle(0,topicname);
                fm.beginTransaction().replace(R.id.frament, Fragment_ThematicAList.newInstance(need_id)).commit();
                break;
            case "品牌商品列表":
                String catname = getIntent().getStringExtra("catname");
                String top = getIntent().getStringExtra("top");
                initTitle(0,catname);
                fm.beginTransaction().replace(R.id.frament, Fragment_BrandGoodsList.newInstance(top)).commit();
                break;
            case "海淘商品列表":
                String nid = getIntent().getStringExtra("nid");
                int differ = getIntent().getIntExtra("differ",0);
                fm.beginTransaction().replace(R.id.frament, Fragment_SeaAmoyLClIST.newInstance(differ,nid)).commit();
                break;
            case "品牌折扣":
            case "地道中国":
            case "特色中国":
                fm.beginTransaction().replace(R.id.frament, Fragment_BrandDiscountMain.newInstance()).commit();
                break;
            case "团长免单":
            case "免单开团":
                fm.beginTransaction().replace(R.id.frament, Fragment_ColonelFree.newInstance(need_id)).commit();
                break;
            case "众筹尝鲜":
                fm.beginTransaction().replace(R.id.frament, Fragment_CFundingTaste.newInstance(need_id)).commit();
                break;
            case "新品预售":
                fm.beginTransaction().replace(R.id.frament, Fragment_CFundingTaste.newInstance(need_id)).commit();
                break;
            case "店铺街":
                fm.beginTransaction().replace(R.id.frament, Fragment_StoreStreet.newInstance()).commit();
                break;
            case "低价清仓":
            case "底价清仓":
            case "品牌清仓":
                fm.beginTransaction().replace(R.id.frament, Fragment_BrandClearance.newInstance(need_id)).commit();
                break;
            case "国家馆":
                String country_name = getIntent().getStringExtra("country_name");
                initTitle(0,country_name);
                fm.beginTransaction().replace(R.id.frament, Fragment_SACountry.newInstance(need_id)).commit();
                break;
            case "同城新零售":
            case "同城":
            case "同城购":
            case "同城商品":
                returncity = LoginState.getInstance().getUserInfo(mcontext, 3);
                main_top_citychoose.setVisibility(View.VISIBLE);
                if(!TextUtils.isEmpty(returncity)){
                    main_top_citychoose.setText(returncity);
                    main_top_title.setVisibility(View.GONE);
                }
                fm.beginTransaction().replace(R.id.frament, Fragment_SameCity_Service.newInstance()).commit();
                break;
            case "我的粉丝":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Fans.newInstance()).commit();
                break;
            case "关于我们":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_AboutUs.newInstance()).commit();
                break;
            case "热搜":
            case "热销榜":
                fm.beginTransaction().replace(R.id.frament, Fragment_EveryBodyBuy.newInstance()).commit();
                break;
            case "精选专题":
            case "精选新品":
                fm.beginTransaction().replace(R.id.frament, Fragment_ChoiceTopic.newInstance()).commit();
                break;
            case "地区列表":
                String catName = getIntent().getStringExtra("catname");
                String topid = getIntent().getStringExtra("top");
                initTitle(0,catName);
                fm.beginTransaction().replace(R.id.frament, Fragment_TabBrandGoodsList.newInstance(topid)).commit();
                break;
            case "9.9元包邮":
            case "9.9包邮":
            case "9.9特卖":
                fm.beginTransaction().replace(R.id.frament, Fragment_FreePostage.newInstance()).commit();
                break;
            case "积分记录":
                main_top_share.setVisibility(View.INVISIBLE);
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Score_Detail.newInstance()).commit();
                break;
        }

    }

    private WebView web;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (title.equals("")) {
            if (keyCode == KeyEvent.KEYCODE_BACK && web.canGoBack()) {
                String url = web.getUrl();
                if(url.equals("") || url.equals(web_url)){
                    super.onBackPressed();
                }else{
                    web.goBack();
                }
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void SendMessageValue(WebView webview) {
        this.web = webview;
    }


    //跳转到国家馆
    public  void JumptoCoutry(){
        String need_id = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_NEED_ID);
        FragmentManager fm =  getSupportFragmentManager();
        String country_name = getIntent().getStringExtra("country_name");
        initTitle(0,country_name);
        fm.beginTransaction().replace(R.id.frament, Fragment_SACountry.newInstance(need_id)).commit();
    }
    //跳到专题页面
    public void jumptoStair(){
        String need_id = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_NEED_ID);
        FragmentManager fm =  getSupportFragmentManager();
        String topicname = getIntent().getStringExtra("topicname");
        initTitle(0,topicname);
        fm.beginTransaction().replace(R.id.frament, Fragment_ThematicAList.newInstance(need_id)).commit();
    }
}
